package com.mapbased.wfw.components;

import com.mapbased.wfw.*;
import org.dom4j.*;
import com.mapbased.wfw.PageLoader.StringBufferBag;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import com.mapbased.wfw.PageLoader.LoadContext;

public class HtmlEncode
    extends Component
{
    private Binding textBinding;

    /**
     *
     * @param ele Element
     * @param loader PageLoader
     * @return Element
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public Element parseXml(Element ele,PageLoader.LoadContext lc)
    {
        Attribute a=ele.attribute("text");
        if(a == null)
        {
            throw new java.lang.RuntimeException(
                "Cannot find text attribute for HtmlEncode:" +
                ele.getName());
        }
        String s=a.getValue();
        ele.remove(a);

        LoadContext nlc=new LoadContext(lc);
        List l=nlc.segments;
        lc.loader.parseBinding(s,nlc);
        for(int i=0;i < l.size();i++)
        {
            Object o=l.get(i);
            if(o instanceof Binding)
            {
                this.textBinding=(Binding)o;
                return null;
            }
        }
        throw new java.lang.RuntimeException(
            "Cannot find binding for Condition:" +
            ele.getName());

    }

    /**
     * render
     *
     * @param context RenderContext
     * @throws IOException
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void render(RenderContext context)
        throws IOException
    {
        Object o=this.textBinding.getValue(context);
        String s=String.valueOf(o == null ? "" : o);
        s=com.mapbased.wfw.Util.replaceToXml(s);
        context.getOutputWriter().write(s);

    }

    /**
     * renderHeader
     *
     * @param context RenderContext
     * @param sb StringBuffer
     * @param m Map
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void renderHeader(RenderContext context,StringBuffer sb,Map m)
    {
    }
}
